/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/9/19 7:24 下午
 * @version V1.0
 */

import { pathToRegexp } from 'path-to-regexp';
import { message } from 'antd';
import {
    reqGetPcategory,
    reqProductsList,
    reqDeleteProduct,
    reqUpdateProduct,
    reqNewsList,
    reqDeleteNews,
    reqStatisticsNews
} from 'api/index';

export default {
    namespace: 'ProductList',
    state: {
        dataSource: {},
        keyword: '',
        limit: 10,
        offset: 0,
        category_id: '',
        categoryOption: [],
        statistics: {
            total_audited: 0,
            total_has_no_audit: 0
        },
        audit: 1, //默认未审核状态
    },
    reducers: {
        setProductList(state, { payload: { dataSource, keyword, limit, offset, category_id } }) {
            return { ...state, dataSource, keyword, limit, offset, category_id };
        },
        setCategoryOption(state, { payload: categoryOption }) {
            return { ...state, categoryOption };
        },
        setStatistics(state, { payload: statistics }) {
            return { ...state, statistics };
        },
        setAudit(state, { payload }) {
            return { ...state, audit: payload };
        }
    },
    effects: {
        *getNewsStatistics({ payload }, { call, put, select }) {
            const result = yield call(reqStatisticsNews);
            if (result.status === 1) {
                yield put({
                    type: 'setStatistics',
                    payload: result.data
                });
            } else {
                message.error(result.message);
            }
        },
        * getProductList({ payload }, { call, put, select }) {
            const { ProductList } = yield select();
            const postData = Object.assign(payload, { audit: ProductList.audit, })
            const result = yield call(reqNewsList, postData);
            if (result.status === 1) {
                const dataSource = result.data;
                yield put({
                    type: 'setProductList',
                    payload: {
                        dataSource,
                        keyword: payload.keyword || '',
                        limit: payload.limit || 10,
                        offset: payload.offset || 0,
                        audit: ProductList.audit,
                    },
                });
            } else {
                message.error(result.message);
            }
        },
        * getCategoryOption({ _ }, { call, put, select }) {
            const result = yield call(reqGetPcategory);
            if (result.status === 1) {
                const categoryOption = result.data;
                yield put({
                    type: 'setCategoryOption',
                    payload: categoryOption,
                });
            } else {
                message.error(result.message);
            }
        },
        * removeProduct({ payload, callback }, { call, put, select }) {
            const result = yield call(reqDeleteNews, payload);
            if (result.status === 1) {
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
        * updateProduct({ payload, callback }, { call, put, select }) {
            const result = yield call(reqUpdateProduct, payload);
            if (result.status === 1) {
                if (callback && typeof callback === 'function') {
                    callback(result);
                }
            } else {
                message.error(result.message);
            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname }) => {
                if (pathToRegexp('/news-manage/news/list').exec(pathname)) {
                    dispatch({
                        type: 'getProductList',
                        payload: {}
                    });
                    dispatch({
                        type: 'getCategoryOption',
                    });
                    dispatch({
                        type: 'getNewsStatistics'
                    });
                }
            });
        },
    },
};
